import {defineStore} from 'pinia'
import {ref} from 'vue'
import axios from 'axios'

export interface UserInfo {
    id?: number;
    userAccount?: string;
    username?: string;
    avatarUrl?: string;
    userRole?: string;
    phone?: string;
    updateTime?: string;
}

export const useUserStore = defineStore('user', () => {
    /**
     * 用户信息
     */
    const userInfo = ref<UserInfo | null>(null)
    /**
     * 登录状态
     */
    const authState = ref(false)
    /**
     * 用户信息框状态
     */
    const userInfoBoxState = ref(false)
    const fetchUserInfo = async () => {
        try {
            const response = await axios.get('/api/user/current')
            console.log(response.data.data)
            userInfo.value = response.data.data
            if (userInfo.value == null) {
                authState.value = false
                return;
            } else {
                authState.value = true
            }
            console.log(userInfo.value)
        } catch (error) {
            authState.value = false
        }
        console.log(authState.value)
    }


    return {userInfo, authState, fetchUserInfo, userInfoBoxState}
})